package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class MsgDetailActivityBack extends AppCompatActivity {
    Intent intent;
    JSONObject jsonObject,response;
    TextView textView;
    ImageView imageView;
    StringBuilder stringBuilder=new StringBuilder();
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_detail_back);
        textView=findViewById(R.id.textView_msg2);
        imageView=findViewById(R.id.imageView2);

        intent=getIntent();
        Bundle extras = intent.getExtras();
        String BackJson= (String) extras.get("responseBack");
        Bitmap imageBack = (Bitmap) extras.get("imageBack");
        createText(BackJson);

        textView.setText(stringBuilder);
        imageView.setImageBitmap(imageBack);

    }

    private void createText(String jsonString){
        try {
            jsonObject = new JSONObject(jsonString);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        try {
            response = jsonObject.getJSONObject("Response");
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        HashMap<String, Object> hashMap = new HashMap<>();
        JSONObject finalResponse = response;
        response.keys().forEachRemaining(key -> {
            try {
                if(key.equals("ValidDate") || key.equals("Authority")){
                    hashMap.put(key, finalResponse.get(key));
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        });

        for (String key : hashMap.keySet()) {
            Object value = hashMap.get(key);
            assert value != null;
            stringBuilder.append(key).append(": ").append(value.toString()).append("\n"); // 添加键值对到StringBuilder，并换行
        }
    }
}